//
//  YBBuyPopupVC.swift
//  Moonlight
//
//  Created by Penlon Kim on 2024/1/5.
//  Copyright © 2024 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit
import SwiftyJSON
import SDWebImage
import SVProgressHUD

class YBBuyPopupVC: BaseMaskVC {
    
    // MARK: Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(bgImgView)
        bgImgView.addSubviews([titleLabel, closeBtn, qrCodeImgView, savePhotoBtn])
        
        bgImgView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(kScreenWidth*0.693)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(16).priority(.high)
            make.height.equalTo(18)
        }
        
        closeBtn.snp.makeConstraints { make in
            make.size.equalTo(15)
            make.right.equalTo(-10)
            make.top.equalTo(12)
        }
        
        qrCodeImgView.snp.makeConstraints { make in
            make.size.equalTo(kScreenWidth*0.284)
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
        }
        
        savePhotoBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(qrCodeImgView.snp.bottom).offset(6)
            make.bottom.equalTo(-11)
        }
        
        // 获取图片
        vm.req_AppBanner(CHANNEL_KUKA_IOS.string) { [weak self] ok, obj, msg, code in
            if ok {
                guard let self = self,
                      let js = obj as? JSON,
                      let data = js[NetDataKey].string else { return }
                self.qrCodeImgView.sd_setImage(with: URL(string: data.urlEncode))
            } else {
                HintTool.toast(msg)
            }
        }
        
    }
    
    // MARK: - lazy
    lazy var vm: YBHomeViewModel = {
        let vm = YBHomeViewModel()
        return vm
    }()
    
    private lazy var bgImgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_popup_bg"))
        iv.isUserInteractionEnabled = true
        iv.bk_addCornerBorder(radius: 8, borderWidth: 0.1, borderColor: .clear)
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var closeBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "icon_popup_close"), for: .normal)
        btn.bk_addTarget { [weak self] sender in
            self?.dismiss(animated: true, completion: nil)
        }
        return btn
    }()
    
    private lazy var titleLabel = self.bk_addLabel(text: "微信扫一扫购买", font: .systemFont(ofSize: 16, weight: .medium), bgColor: .clear, textColor: ColorDef.dark27, align: .center)
    
    private lazy var qrCodeImgView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "icon_popup_qrcode"))
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var savePhotoBtn: UIButton = {
        let btn = self.bk_addButton(type: .custom, title: "图片保存到本地", font: .systemFont(ofSize: 14), bgColor: .clear, titleColor: ColorDef.gray153)
        btn.bk_addTarget { [weak self] sender in
            guard let self = self else { return }
            BKPermission.request(.photoLibrary) { [unowned self] status in
                if status.isAuthorized {
                    HintTool.toast(.Success.save)
                    self.qrCodeImgView.image?.bk_saveToPhotosAlbum()
                } else {
                    HintTool.toast(.canNotVisitPhoto)
                }
            }
        }
        return btn
    }()
    
}
